package com.zx._01_基础._015_编码技巧;

import java.text.ParseException;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.time.DateFormatUtils;
import org.apache.commons.lang3.time.DateUtils;
import org.junit.Test;

@Slf4j
public class 时间 {

    /**
     * @title 时间格式化(LocalDateTime是数据安全的) (JDK8)
     * @author: xue.zhang
     * @date 2019年2月15日下午8:20:15
     */
    @Test
    public void test01() {
        final LocalDateTime time = LocalDateTime.now();

        System.out.println(DateTimeFormatter.BASIC_ISO_DATE.format(time));

        System.out.println(DateTimeFormatter.ISO_DATE.format(time));
        System.out.println(DateTimeFormatter.ISO_DATE_TIME.format(time));

//		System.out.println(DateTimeFormatter.ISO_INSTANT.format(time));

        System.out.println(DateTimeFormatter.ISO_LOCAL_DATE.format(time));
        System.out.println(DateTimeFormatter.ISO_LOCAL_DATE_TIME.format(time));
        System.out.println(DateTimeFormatter.ISO_LOCAL_TIME.format(time));

//		System.out.println(DateTimeFormatter.ISO_OFFSET_DATE.format(time));
//		System.out.println(DateTimeFormatter.ISO_OFFSET_DATE_TIME.format(time));
//		System.out.println(DateTimeFormatter.ISO_OFFSET_TIME.format(time));

        System.out.println(DateTimeFormatter.ISO_ORDINAL_DATE.format(time));
        System.out.println(DateTimeFormatter.ISO_TIME.format(time));
        System.out.println(DateTimeFormatter.ISO_WEEK_DATE.format(time));
//		System.out.println(DateTimeFormatter.ISO_ZONED_DATE_TIME.format(time));
//		System.out.println(DateTimeFormatter.RFC_1123_DATE_TIME.format(time));

        final DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        System.out.println(df.format(time));
    }

    /**
     * @title 时间计算(DateTimeFormatter是数据安全的)(JDK8)
     * @author: xue.zhang
     * @date 2019年2月15日下午8:20:31
     */
    @Test
    public void test02() {
        final LocalDateTime time = LocalDateTime.now();
        // 常用方法1
        final LocalDateTime of1 = LocalDateTime
                .of(time.getYear(), time.getMonth(), time.getDayOfMonth(), time.getHour(), time.getMinute());

        // 常用方法2
        final LocalDateTime of2 = LocalDateTime
                .of(time.getYear(), time.getMonth(), time.getDayOfMonth(), time.getHour(), time.getMinute(),
                        time.getSecond());

        // str --> date
        final DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        final LocalDateTime date = LocalDateTime.parse("2019-02-15 15:15:01", df);
        System.out.println(date);

        // date --> str
        final String str = df.format(time);
        System.out.println(str);

    }

    /**
     * 使用commons-lang3
     * 格式化时间
     */
    @Test
    public void test03() throws ParseException {
        // 最简单的方式如下：
        // Date类型转String类型
        String dateStr1 = DateFormatUtils.format(new Date(), "yyyy-MM-dd HH:mm:ss");
        System.out.println(dateStr1); // 输出 2021-05-01 01:01:01

        // String类型转Date类型
        Date date1 = DateUtils.parseDate("2021-05-01 01:01:01", "yyyy-MM-dd HH:mm:ss");

        // 计算一个小时后的日期
        Date date2 = DateUtils.addHours(new Date(), 1);
    }

    /**
     *
     */
    @Test
    public void test04() {
        log.info("blank={}", 1);
    }

    /**
     *
     */
    @Test
    public void test05() {
        log.info("blank={}", 1);
    }
}
